﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PiaoanWeb.AppLogic.DataAccessLayer;
namespace PiaoanWeb.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class BlocksBLL : IDisposable
    {
        DatabaseContext _DatabaseContext;
        public BlocksBLL():this(new DatabaseContext())
        { 
        }

        public BlocksBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public BlockContent GetBlockContentByBlockName(string blockName)
        {
            BlockContent blockContent = _DatabaseContext.BlockContent.Where(b => b.BlockName.Equals(blockName)).FirstOrDefault();
            if (blockContent == null)
            {
                blockContent = new BlockContent();
                blockContent.BlockName = blockName;
                blockContent.Content = "";
                _DatabaseContext.BlockContent.Add(blockContent);
                _DatabaseContext.SaveChanges();
                blockContent = _DatabaseContext.BlockContent.Where(b => b.BlockName.Equals(blockName)).FirstOrDefault();
            }
            return blockContent;
        }

        public void Save()
        {
            _DatabaseContext.SaveChanges();
        }
    }
}